package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.TransLogReq;
import sz.szsmk.citizencard.model.network.bean.response.QueryCardAccInfoRes;
import sz.szsmk.citizencard.model.network.bean.response.TransLogRes;
import sz.szsmk.citizencard.mvp.contract.ITransLogContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/18 20:46
 * description:
 */
public class TransLogPresenter extends IBasePresenter
        implements ITransLogContact.ITransLogPresenter<ITransLogContact.ITransLogView> {


    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public TransLogPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void getTransLog(String cardNo, String id, int currentPager) {
        TransLogReq transLogReq = new TransLogReq();
        transLogReq.setAccount_type(id);
        transLogReq.setCard_no(cardNo);
        transLogReq.setPage_num(currentPager + "");
        transLogReq.setPage_size("10");
        transLogReq.setRequest_type("0");
        String json = new Gson().toJson(transLogReq);
        L.e(Constant.TAG, "transLogReq-----------:" + json);
        mNetworkDateSource.queryTransRecords(transLogReq, new SubscribeResult<TransLogRes>() {
            @Override
            protected void onOk(TransLogRes response) {
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "transLogRes----" + toJson);
                ((ITransLogContact.ITransLogView) mView).getTransLogSucc(response);
            }

            @Override
            protected void onNext(TransLogRes response) {
                super.onNext(response);
                ((ITransLogContact.ITransLogView) mView).getTransLogFaie(response.getMessage());
            }

            @Override
            protected void onFailure(TransLogRes response) {
                super.onFailure(response);
                ((ITransLogContact.ITransLogView) mView).getTransLogFaie(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });


    }

    @Override
    public void queryCardAcctInfo(String cardNo) {
        TransLogReq transLogReq = new TransLogReq();
        transLogReq.setCard_no(cardNo);
        String json = new Gson().toJson(transLogReq);
        mNetworkDateSource.queryCardAcctInfo(transLogReq, new SubscribeResult<QueryCardAccInfoRes>() {
            @Override
            protected void onOk(QueryCardAccInfoRes response) {
                ((ITransLogContact.ITransLogView) mView).queryCardAcctInfoSucc(response);
            }

            @Override
            protected void onFailure(QueryCardAccInfoRes response) {
                super.onFailure(response);
                ((ITransLogContact.ITransLogView) mView).queryCardAcctInfoFail();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

}
